package rpg.uiView.shortCut
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	import rpg.Utils.Keycode;
	import rpg.core.AutoCastSpellComponent;
	import rpg.data.Spell;
	import rpg.manager.GameManager;
	import rpg.manager.UIManager;
	import rpg.uiView.Ico;

	import ui.Component;


	public class ShortCutView extends Component
	{
		private var mainUi : Sprite;
		/**
		 * 快捷栏的键码，只能设置第一栏
		 */
		private var shortcutKeys : Array = [Keycode.Keyb1 , Keycode.Keyb2 , Keycode.Keyb3 , Keycode.Keyb4 , Keycode.Keyb5 , Keycode.Keyb6 , Keycode.Keyb7 , Keycode.Keyb8 , Keycode.Keyb9 , Keycode.Keyb0 , Keycode.BackQuote , Keycode.Minus];

		/**
		 * 快捷键对应的技能
		 */
		public var shortCuts : Dictionary = new Dictionary();

		private var shortCutIocs : Array = [];

		public function ShortCutView()
		{
			super();
		}

		override public function init() : void
		{
			mainUi = new UIManager.shortcutUi();
			this.addChild(mainUi);
			mainUi.removeChild(mainUi["bar3"]);
			initBar(1 , shortcutKeys);
			initBar(2 , null);
			GameManager.getInstance().selfRole.shortCutIocs = shortCutIocs;
		}

		private function initBar(index : int , keys : Array = null) : void
		{
			for(var i : int = 0 ; i < 10 ; i++)
			{
				var icoBox : Sprite = mainUi["bar" + index]["ico" + i];
				var code : int = -1;
				icoBox["keyTxt"].visible = keys != null;
				if(keys)
				{
					code = keys[i];
					icoBox["keyTxt"]["txt"].text = Keycode.keyName[code];
				}
				icoBox["keyTxt"]["txt"].x += 1;
				icoBox["icoContainer"]["containerDefault"].visible = false;
				icoBox["icoContainer"]["containerRollover"].visible = false;
				var ico : Ico = new Ico(Ico.ICO_SPELL);
				shortCutIocs.push(ico);
				ico.move(2 , 2);
				icoBox["icoContainer"].addChild(ico);
				if(code != -1)
					shortCuts[code] = ico;
			}
		}

		override public function addListener() : void
		{
			for each(var ico : Ico in shortCutIocs)
			{
				ico.addEventListener(MouseEvent.CLICK , onIcoClick);
			}
		}

		override public function removeListener() : void
		{
			for each(var ico : Ico in shortCutIocs)
			{
				ico.removeEventListener(MouseEvent.CLICK , onIcoClick);
			}
		}

		override public function show() : void
		{
			reset();
			var heroSpellList : Array = GameManager.getInstance().heroSpellList;
			var len : int = heroSpellList.length;
			for(var i : int = 0 ; i < len ; i++)
			{
				if(i > shortCutIocs.length)
					break;
				shortCutIocs[i].data = heroSpellList[i];
			}
		}

		private function onIcoClick(evt : MouseEvent) : void
		{

		}

		public function onShortcurtKey(key : int) : void
		{
			var index : int = shortcutKeys.indexOf(key);
			if(index == -1)
				return;
			var ico : Ico = shortCuts[key];
			if(!ico)
				return;
			AutoCastSpellComponent(GameManager.getInstance().selfRole.getComponent(AutoCastSpellComponent)).castSpell(ico.data as Spell);
		}

		override public function reset() : void
		{
			this.move((stage.stageWidth - width) * .5 , stage.stageHeight);
		}
	}
}